﻿using Domain.Entitys;
using Domain.Repositorys;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFCore.Repositorys
{
    public class RolesRepositoty : IRolesRepositoty
    {
        private readonly MyDbContext myDbContext;

        public RolesRepositoty(MyDbContext myDbContext)
        {
            this.myDbContext = myDbContext;
        }

        public Task<List<Cascader>> GetCFFKCFSKCFTK()
        {
            throw new NotImplementedException();
        }

        public List<Roles> GetRoles(int a, int b)
        {
            return myDbContext.Roles.ToList().Skip((a - 1) * b).Take(b).ToList();
        }

        public int GetRolesCount()
        {
            return myDbContext.Roles.ToList().Count();
        }
        /// <summary>
        /// 角色管理表添加
        /// </summary>
        /// <param name="roles"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> RolesAdd(Roles roles)
        {
             await myDbContext.Roles.AddAsync(roles);
            return await myDbContext.SaveChangesAsync();
        }
        /// <summary>
        /// 用户列表分页数据显示
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public List<User> GetUser(int a, int b)
        {
            return myDbContext.Users.ToList().Skip((a - 1) * b).Take(b).ToList();
        }


        public int GetUserCount()
        {
            return myDbContext.Users.ToList().Count();
        }

        public List<Roles> GetRole2()
        {
            return myDbContext.Roles.ToList();
        }

        public int UserAdd(User user)
        {
            myDbContext.Users.Add(user);
            return myDbContext.SaveChanges();
        }
        public User GetUser2(int uid)
        {
            return myDbContext.Users.Find(uid);
        }

        public int UserUpdate(User user)
        {
            myDbContext.Users.Update(user);
            return myDbContext.SaveChanges();
        }

        public int UserDelete(int uid)
        {
            User user = myDbContext.Users.Find(uid);
            myDbContext.Users.Remove(user);
            return myDbContext.SaveChanges();
        }


        public Roles GetRole(int id)
        {
            return myDbContext.Roles.FirstOrDefault(e => e.RolesID == id);
        }

        public List<Jurisdiction> GetQX()
        {
            List<Jurisdiction> list = myDbContext.Jurisdictions.Where(e => e.GroupID == 0).ToList();
            for (int i = 0; i < list.Count; i++)
            {
                list[i].children = GetChildren(list[i].JuriID);
            }
            return list;
        }
        public List<Jurisdiction> GetChildren(int pid)
        {
            List<Jurisdiction> list = myDbContext.Jurisdictions.Where(e => e.GroupID == pid).ToList();
            for (int i = 0; i < list.Count; i++)
            {
                list[i].children = GetChildren(list[i].JuriID);
            }
            return list;
        }

        public int Delete(int id)
        {
            List<RolesJurisdiction> list = myDbContext.RolesJurisdictions.Where(e => e.RolesID == id).ToList();
            foreach (RolesJurisdiction i in list)
            {
                myDbContext.RolesJurisdictions.Remove(i);
                myDbContext.SaveChanges();
            }
            return list.Count;
        }

        public int Add(int id, int id2)
        {
            RolesJurisdiction roleJurisdiction = new RolesJurisdiction();
            roleJurisdiction.RolesID = id;
            roleJurisdiction.JuriID = id2;
            myDbContext.RolesJurisdictions.Add(roleJurisdiction);
            return myDbContext.SaveChanges();
        }

        public int[] GetQXById(int id)
        {
            List<Jurisdiction> list = myDbContext.Jurisdictions.Where(e => e.JurisdictionJuriID == 100).ToList();
            List<RolesJurisdiction> list2 = myDbContext.RolesJurisdictions.Where(e => e.RolesID == id).ToList();
            List<int> list3 = new List<int>();
            foreach (RolesJurisdiction i in list2)
            {
                foreach (Jurisdiction j in list)
                {
                    if (i.JuriID == j.JuriID)
                    {
                        list3.Add(i.JuriID);
                    }
                }
            }
            int[] array = list3.ToArray();
            return array;
        }
        public List<XQ> GetQX2(string str)
        {
            Roles role = myDbContext.Roles.Where(e => e.RolesID.ToString() == str).ToList()[0];
            List<RolesJurisdiction> array = myDbContext.RolesJurisdictions.Where(e => e.RolesID == role.RolesID).OrderBy(e => e.JuriID).ToList();
            List<Jurisdiction> list = new List<Jurisdiction>();
            for (int i = 0; i < array.Count; i++)
            {
                Jurisdiction jurisdiction = myDbContext.Jurisdictions.Find(array[i].JuriID);
                list.Add(jurisdiction);
            }
            return GetChildren2(0, list);
        }

        public List<XQ> GetChildren2(int pid, List<Jurisdiction> listp)
        {
            List<Jurisdiction> list = listp.Where(e => e.GroupID == pid).ToList();
            List<XQ> list2 = new List<XQ>();
            for (int i = 0; i < list.Count; i++)
            {
                XQ qx = new XQ()
                {
                    Label = list[i].JurName,
                    Url = list[i].JurAddress,
                    Children = GetChildren2(list[i].JuriID, listp),
                };
                list2.Add(qx);
            }
            return list2;
        }

        /// <summary>
        /// 修改角色信息
        /// </summary>
        /// <param name="roles"></param>
        /// <returns></returns>
        public int RolesUpdate(Roles roles)
        {
            myDbContext.Roles.Update(roles);
            return myDbContext.SaveChanges();
        }

        /*    public int[] GetQXById(int id)
            {
                List<Jurisdiction> list = myDbContext.Jurisdictions.Where(e => e.JurisdictionJuriID == 100).ToList();
                List<RoleJurisdiction> list2 = myDbContext.RoleJurisdictions.Where(e => e.RolesID == id).ToList();
                List<int> list3 = new List<int>();
                foreach (RoleJurisdiction i in list2)
                {
                    foreach (Jurisdiction j in list)
                    {
                        if (i.JuriID == j.JuriID)
                        {
                            list3.Add(i.JuriID);
                        }
                    }
                }
                int[] array = list3.ToArray();
                return array;
            }*/

        /*  public List<Qx> GetQX2(string str)
          {
              Role role = myDbContext.Roles.Where(e => e.RolesName == str).ToList()[0];
              List<RoleJurisdiction> array = myDbContext.RoleJurisdictions.Where(e => e.RolesID == role.RolesID).OrderBy(e => e.JuriID).ToList();
              List<Jurisdiction> list = new List<Jurisdiction>();
              for (int i = 0; i < array.Count; i++)
              {
                  Jurisdiction jurisdiction = myDbContext.Jurisdictions.Find(array[i].JuriID);
                  list.Add(jurisdiction);
              }
              return GetChildren2(0, list);
          }

          public List<Qx> GetChildren2(int pid, List<Jurisdiction> listp)
          {
              List<Jurisdiction> list = listp.Where(e => e.GroupID == pid).ToList();
              List<Qx> list2 = new List<Qx>();
              for (int i = 0; i < list.Count; i++)
              {
                  Qx qx = new Qx()
                  {
                      Label = list[i].JurName,
                      Url = list[i].JurAddress,
                      Children = GetChildren2(list[i].JuriID, listp),
                  };
                  list2.Add(qx);
              }
              return list2;
          }*/
    }
}
